package com.migu.contacts.main;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.frank.libandroid.BaseActivity;
import com.frank.libandroid.DataStore;
import com.frank.libandroid.Key;
import com.migu.contacts.bean.ContactBean;
import com.migu.contacts.databinding.ActivityCycleBinding;
import com.migu.contacts.util.CheckBoxUtil;
import com.migu.contacts.util.PopUtil;
import com.migu.contacts.widget.Callback;
import com.migu.contacts.widget.Label2;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class CycleActivity extends BaseActivity<ActivityCycleBinding> {
    public static final Key<ContactBean.CycleTip> KEY_CYCLE = new Key<>();

    Label2.LabelBean labelBean = new Label2.LabelBean("提醒日期", "", 0, true);

    @Override
    protected void initData() {
        super.initData();
        Log.d(TAG, "initData() called");
        mBinding.dateTip.setBean(labelBean);
        mBinding.dateTip.mBinding.ivArrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PopUtil.showDatePicker(mActivity, new Callback<String>() {
                    @Override
                    public void onComplete(String s) {
                        mBinding.dateTip.mBinding.tvValue.setText(s);
                    }
                });
            }
        });
    }


    @Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
    public void onMessage(ContactBean.CycleTip cycleTip) {
        Log.d(TAG, "onMessage() called with: cycleTip = [" + cycleTip + "]");
        if (cycleTip.tipStatus) {
            mBinding.noticeSwitch.setChecked(true);
        }
        String tipTime = cycleTip.tipTime;
        if (!TextUtils.isEmpty(tipTime)) {
            String[] s = tipTime.split(" ");
            if (s.length == 2) {
                labelBean.right = s[0];
                Log.d(TAG, "onMessage() called with: cycleTip = [" + s[0] + "]");
                mBinding.dateTip.setBean(labelBean);
            }
        }
//        1=天 2=周 3=月 4=年
        if (cycleTip.cycleType == 1) {
            if (cycleTip.cycleTime == 3) {
                mBinding.btn1.setChecked(true);
            } else if (cycleTip.cycleTime == 7) {
                mBinding.btn2.setChecked(true);
            } else {
                mBinding.dayCount.setText(String.valueOf(cycleTip.cycleTime));
            }
        } else if (cycleTip.cycleType == 3) {
            mBinding.btn3.setChecked(true);
        }
    }

    @Override
    protected void initView() {
        super.initView();
        CheckBoxUtil.checkOne(mBinding.btn1, mBinding.btn2, mBinding.btn3);
    }

    public void save(View view) {
        ContactBean.CycleTip cycleTip = new ContactBean.CycleTip();
        cycleTip.tipStatus = mBinding.noticeSwitch.isChecked();
        String string = mBinding.dateTip.mBinding.tvValue.getText().toString().trim();
        if (!TextUtils.isEmpty(string)) {
            cycleTip.tipTime = string + " 09:00:00";
        }
//        1=天 2=周 3=月 4=年
        if (mBinding.btn1.isChecked()) {
            cycleTip.cycleType = 1;
            cycleTip.cycleTime = 3;
        } else if (mBinding.btn2.isChecked()) {
            cycleTip.cycleType = 1;
            cycleTip.cycleTime = 7;
        } else if (mBinding.btn3.isChecked()) {
            cycleTip.cycleType = 3;
            cycleTip.cycleTime = 1;
        } else {
            String trim = mBinding.dayCount.getText().toString().trim();
            if (!TextUtils.isEmpty(trim)) {
                try {
                    cycleTip.cycleTime = Integer.parseInt(trim);
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
                cycleTip.cycleType = 1;
            }
        }
        DataStore.put(KEY_CYCLE, cycleTip);
        setResult(RESULT_OK);
        finish();
    }
}